package zhihui.mobile.ui.shop.order;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import zhihui.mobile.R;
import zhihui.mobile.bean.shop.SkuInfo;
import zhihui.mobile.config.Config;

/**
 * Created by 张公铭 on 2017/9/1 0001.
 */

public class OrderAdapter extends BaseAdapter
{
    private Context mContext;
    private LayoutInflater inflater;
    private List<SkuInfo> mList = new ArrayList<>();

    public OrderAdapter(Context context)
    {
        mContext = context;
        inflater = LayoutInflater.from(mContext);
    }

    public void setmList(List<SkuInfo> list)
    {
        if (mList == null)
        {
            mList = new ArrayList<>();
        }
        mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount()
    {
        return mList.size();
    }

    @Override
    public Object getItem(int position)
    {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position)
    {
        return mList.get(position).getSkuId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        TextView tv_name, tv_specification, tv_price, tv_num;
        ImageView iv_icon;
        if (convertView == null)
        {
            convertView = inflater.inflate(R.layout.shop_order_details_item, null);
            tv_name = (TextView) convertView.findViewById(R.id.tv_shop_order_details_item_name);
            tv_specification = (TextView) convertView.findViewById(R.id.tv_shop_order_details_item_specification);
            tv_price = (TextView) convertView.findViewById(R.id.tv_shop_order_details_item_price);
            tv_num = (TextView) convertView.findViewById(R.id.tv_shop_order_details_item_num);
            iv_icon = (ImageView) convertView.findViewById(R.id.iv_shop_order_details_item_img);

            ViewCache viewCache = new ViewCache();
            viewCache.tv_name = tv_name;
            viewCache.tv_specification = tv_specification;
            viewCache.tv_price = tv_price;
            viewCache.tv_num = tv_num;
            viewCache.iv_icon = iv_icon;

            convertView.setTag(viewCache);
        } else
        {
            ViewCache viewCache = (ViewCache) convertView.getTag();
            tv_name = viewCache.tv_name;
            tv_specification = viewCache.tv_specification;
            tv_num = viewCache.tv_num;
            tv_price = viewCache.tv_price;
            iv_icon = viewCache.iv_icon;
        }

        SkuInfo skuInfo =mList.get(position);
        tv_name.setText(skuInfo.getTitle());
        tv_price.setText("￥" + ((double)skuInfo.getPrice()/100));
        tv_specification.setText(skuInfo.getSpecif().replace(",", "  "));
        tv_num.setText("×" + skuInfo.getNum());
        ImageLoader.getInstance().displayImage(Config.FILE_URL + skuInfo.getThumbnail(), iv_icon);
        return convertView;
    }

    class ViewCache
    {
        public TextView tv_name, tv_specification, tv_price, tv_num;
        public ImageView iv_icon;
    }
}
